package net.thxjj.adminServer.service.impl;

import net.thxjj.adminServer.dao.DeviceDao;
import net.thxjj.adminServer.entity.Const;
import net.thxjj.adminServer.entity.aep.LoginData;
import net.thxjj.adminServer.entity.aep.profilegc.ProfileGCLoginBean;
import net.thxjj.adminServer.entity.onenet.ModuleBean;
import net.thxjj.adminServer.entity.onenet.OnenetTransResultBean;
import net.thxjj.adminServer.entity.onenet.OnetEvent;
import net.thxjj.adminServer.mongodao.MongoLoginDao;
import net.thxjj.adminServer.mongodao.MongoOnenetDao;
import net.thxjj.adminServer.mongodao.MongoProfileGCLoginDao;
import net.thxjj.adminServer.service.IMainSurveyService;
import net.thxjj.adminServer.util.ParameterMap;
import net.thxjj.adminServer.util.Tools;
import net.thxjj.sitermodel.Device;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MainSurveyService implements IMainSurveyService {
    @Autowired
    private DeviceDao deviceDao;
    @Autowired
    private MongoOnenetDao mongoOnenetDao;
    @Autowired
    private MongoProfileGCLoginDao mongoProfileGCLoginDao;
    @Autowired
    private MongoLoginDao mongoLoginDao;


    @Override
    public List<ParameterMap> getNetStatusStatics() {
        List<ParameterMap>  list = deviceDao.getStaticsNetStatus();
        int count_online = 0;
        int count_offline = 0;
        for (int i=0;i<list.size();i++){
            ParameterMap parameterMap= list.get(i);
            long status =Long.parseLong(parameterMap.getString("net_status"));
            long num = Long.parseLong(parameterMap.getString("num"));
            if(status==1){
                count_online+=num;
            }else{
                count_offline+=num;
            }
        }

        ParameterMap parameterMap = new ParameterMap();
        parameterMap.put("name","在线");
        parameterMap.put("value",count_online);

        ParameterMap parameterMap2 = new ParameterMap();
        parameterMap2.put("name","离线");
        parameterMap2.put("value",count_offline);

        List<ParameterMap>  list2 = new ArrayList<ParameterMap>();
        list2.add(parameterMap);
        list2.add(parameterMap2);
        return list2;
    }

    @Override
    public HashMap<String, Object> getSoftHadWareStatics() {
        List<Device> list = deviceDao.getAllDeviceList();
        List<ParameterMap> listtotal = new ArrayList<>();
        List<ParameterMap> listtotal_hard = new ArrayList<>();
        int count_empty = 0;
        int count_empty_hard = 0;
        ParameterMap da = new ParameterMap();
        ParameterMap da_hard = new ParameterMap();
        for (int i=0;i<list.size();i++){

            Device device = list.get(i);
            if(Const.PRODUCT_ID.equals(device.getProdKey())||Const.PROFILE_PRODUCT_ID.equals(device.getProdKey())){
                LoginData loginData = mongoLoginDao.getNewestLoginInfo(device.getDeviceId());
                if(loginData!=null){
                    if(Tools.isEmpty(loginData.getSoftware_version())){
                        count_empty++;
                    }else{
                        if(da.containsKey(loginData.getSoftware_version())){
                            long count = Long.parseLong(da.getString(loginData.getSoftware_version()));
                            count++;
                            da.put(loginData.getSoftware_version(),count);
                        }else{
                            da.put(loginData.getSoftware_version(),1);
                        }

                    }

                    if(Tools.isEmpty(loginData.getHardware_version())){
                        count_empty_hard++;
                    }else{
                        if(da_hard.containsKey(loginData.getHardware_version())){
                            long count = Long.parseLong(da_hard.getString(loginData.getHardware_version()));
                            count++;
                            da_hard.put(loginData.getHardware_version(),count);
                        }else{
                            da_hard.put(loginData.getHardware_version(),1);
                        }
                    }


                }else{
                    count_empty_hard++;
                    count_empty++;
                }

            }else if(Const.ONENET_PRODUCT_ID.equals(device.getProdKey())){
                OnetEvent onetLoginEvent = mongoOnenetDao.getSmokeLoginEvent(device.getDeviceId());
                if(onetLoginEvent!=null){
                    OnenetTransResultBean onenetTransResultBean = onetLoginEvent.transToBean(onetLoginEvent.getEventContent());
                    ModuleBean moduleBean = onenetTransResultBean.getModuleBean();

                    if(moduleBean!=null){
                        if(Tools.isEmpty(moduleBean.getSoftware_version())){
                            count_empty++;
                        }else{
                            if(da.containsKey(moduleBean.getSoftware_version())){
                                long count = Long.parseLong(da.getString(moduleBean.getSoftware_version()));
                                count++;
                                da.put(moduleBean.getSoftware_version(),count);
                            }else{
                                da.put(moduleBean.getSoftware_version(),1);
                            }

                        }

                        if(Tools.isEmpty(moduleBean.getModule_version())){
                            count_empty_hard++;
                        }else{
                            if(da_hard.containsKey(moduleBean.getModule_version())){
                                long count = Long.parseLong(da_hard.getString(moduleBean.getModule_version()));
                                count++;
                                da_hard.put(moduleBean.getModule_version(),count);
                            }else{
                                da_hard.put(moduleBean.getModule_version(),1);
                            }
                        }


                    }else{
                        count_empty_hard++;
                        count_empty++;
                    }


                }else{
                    count_empty++;
                    count_empty_hard++;
                }
            }else if(Const.PROFILE_PRODUCT_ID_GC.equals(device.getProdKey()) || Const.PROFILE_PRODUCT_ID_GC_2.equals(device.getProdKey())){
                ProfileGCLoginBean gcLoginBean = mongoProfileGCLoginDao.getNewestLoginInfo(device.getDeviceId());
                if(gcLoginBean!=null){

                    if(Tools.isEmpty(gcLoginBean.getMD_FwVersion())){
                        count_empty++;
                    }else{
                        if(da.containsKey(gcLoginBean.getMD_FwVersion())){
                            long count = Long.parseLong(da.getString(gcLoginBean.getMD_FwVersion()));
                            count++;
                            da.put(gcLoginBean.getMD_FwVersion(),count);
                        }else{
                            da.put(gcLoginBean.getMD_FwVersion(),1);
                        }
                    }

                   if(Tools.isEmpty(gcLoginBean.getMD_HwVersion())){
                    count_empty_hard++;
                   }else{
                       if(da_hard.containsKey(gcLoginBean.getMD_HwVersion())){
                           long count = Long.parseLong(da_hard.getString(gcLoginBean.getMD_HwVersion()));
                           count++;
                           da_hard.put(gcLoginBean.getMD_HwVersion(),count);
                       }else{
                           da_hard.put(gcLoginBean.getMD_HwVersion(),1);
                       }
                   }


                }else{
                    count_empty++;
                    count_empty_hard++;
                }
            }else{
                count_empty++;
                count_empty_hard++;
            }


        }

        if(count_empty>0)
            da.put("未知",count_empty);

        if(count_empty_hard>0)
            da_hard.put("未知",count_empty_hard);

        Iterator iter = da.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = (String)entry.getKey();
            Object val = entry.getValue();
            ParameterMap d = new ParameterMap();
            d.put("name",key);
            d.put("value",val);
            listtotal.add(d);
        }

        Iterator iter2 = da_hard.entrySet().iterator();
        while (iter2.hasNext()) {
            Map.Entry entry = (Map.Entry) iter2.next();
            String key = (String)entry.getKey();
            Object val = entry.getValue();
            ParameterMap d = new ParameterMap();
            d.put("name",key);
            d.put("value",val);
            listtotal_hard.add(d);
        }

        HashMap<String,Object> result = new HashMap<String,Object>();
        result.put("soft",listtotal);
        result.put("hard",listtotal_hard);

        return result;

    }

}
